<template>
  <div class="wrap" @click="medalUp">
    <div class="medal-box" v-if="true">
      <img class="m-word-img" :class="{show:levelUp}" src="/static/rank/level-up.png">
      <div class="m-medal" @click="handleHideOldMedal">
        <div class="u-img" :class="{shrinkImg:shrinkImgBtn}">
          <img :src="medalImg">
          <div class="u-medal-level">
            <img src="/static/rank/medal-bg.png" alt="">
            <div class="u-medal-name">
              {{medalName}}
              <div class="u-medal-num" :class="{numChange:smallMedalUp}">{{medalNum}}</div>
            </div>
          </div>
        </div>
        <!-- <div class="u-light-mask" :class="{showHalf:showLightMask}"></div> -->
        <div class="u-blue-cycle" :class="{shrink:shrinkBtn}" @animationend="handleShowNewMedal">
          <div class="u-blue-cycle-inner" :class="{shrink:shrinkBtn}"></div>
        </div>
        <div class="u-img" v-if="magnifyBtn" :class="{magnify:magnifyBtn}">
        <div class="u-light-mask" :class="{showHalf:showLightMask}"></div>
          <img :src="newMedalImg">
          <div class="u-medal-level">
            <img src="/static/rank/medal-bg.png" alt="">
            <div class="u-medal-name">
              {{newMedalName}}
              <div class="u-medal-num">{{newMedalNum}}</div>
            </div>
          </div>
        </div>
        <div class="u-light-bg" :class="{hide:!showLightBg}">
          <img src="/static/rank/light-bg.png" alt="">
        </div>

      </div>
      <div class="m-star-list">
        <div class="u-star" v-for="(item,index) in starList" :key="index">
          <img class="u-star-act" :class="{transStar:starTrans,showStar:item.act,downStar:item.down}" src="/static/rank/star-active.png">
        </div>
      </div>
      <div class="u-win-streak" :class="{show:winStreak}">连胜</div>
    </div>
  </div>

</template>

<script>
export default {
  // props:["medalInfo"],
  data() {
    return {
      shrinkBtn: false, //光圈
      shrinkImgBtn: false, //旧图标
      magnifyBtn: false, //新图标 显示
      showLightMask: false, //金色模糊遮罩
      showLightBg: false, //佛光
      starTrans: true, //星星过渡效果
      fullStar: false, //旧段位满星状态
      bigMedalChange: false, //大段变化
      smallMedalUp:false,
      levelUp: false,
      eventOnce: true,
      winStreak: false, //是否连胜
      // mock data
      starList: [], //星星列表
      medalName: "鱼跃龙门",
      medalNum: "Ⅰ",
      medalImg:"https://ggbsq.xingpanwang.com/renren-fast/smallUploadSource//vientianeTestParagraphImage/9c69a212-3c4b-4e01-9dfb-8a4957d3eaa1.jpg",
      newMedalName:"青龙云平",
      newMedalNum: "Ⅰ",
      newMedalImg: "https://ggbsq.xingpanwang.com/renren-fast/smallUploadSource//vientianeTestParagraphImage/7bc4bcde-86e3-4de1-82ca-283162feab12.jpg",

      medalInfo: {
        continueWinNum: 3,
        paragraph: 2,
        paragraphImage: "https://ggbsq.xingpanwang.com/renren-fast/smallUploadSource//vientianeTestParagraphImage/9c69a212-3c4b-4e01-9dfb-8a4957d3eaa1.jpg",
        paragraphName: "鱼跃龙门Ⅴ",
        star: 0
      },
      newData: {
        continueWinNum: 3,
        paragraph: 1,
        paragraphImage: "https://ggbsq.xingpanwang.com/renren-fast/smallUploadSource//vientianeTestParagraphImage/7bc4bcde-86e3-4de1-82ca-283162feab12.jpg",
        paragraphName: "鱼跃龙门Ⅰ",
        star: 3
      }
    };
  },
  watch: {
    medalInfo(_new, _old) {
      // 初始化新段位名字和数字
      this.newMedalName = _new.paragraphName.slice(0,4);
      this.newMedalNum = _new.paragraphName.slice(4);
      //区分 升级 掉级 升小段 掉小段 升大段 掉大段
      _old.star == 3 ? (this.fullStar = true) : null; //判断满星
      _old.paragraphName.slice(0,4) == _new.paragraphName.slice(0,4)? (this.bigMedalChange = false): (this.bigMedalChange = true); //是否大段变化

      _new.paragraph > _old.paragraph ? this.handleMedalUp() : null; //先升级 在升段
      _new.paragraph < _old.paragraph ? this.handleMedalDown() : null;

      if (_new.paragraph == _old.paragraph && _new.star > _old.star) {
        this.handleLevelUp();
        setTimeout(() => {
          this.checkWinStreak();
        }, 500);
      }

      _new.paragraph == _old.paragraph && _new.star < _old.star
        ? this.handleLevelDown()
        : null;
    }
  },
  computed: {},
  methods: {
    medalUp() {
      this.medalInfo = this.newData;
    },
    initStarList() {
      let list = [
        { act: false, down: false },
        { act: false, down: false },
        { act: false, down: false }
      ];
      list.map((item, index) => {
        if (index < this.medalInfo.star) {
          item.act = true;
        }
      });
      this.starList = list;
    },

    initMedalName() {
      this.medalName = this.medalInfo.paragraphName.slice(0, 4);
      this.medalNum = this.medalInfo.paragraphName.slice(4);
    },

    handleMedalUp() {
      this.levelUp = true;
      this.handleLevelUp(); //升级 加一星
      setTimeout(() => {
        this.checkWinStreak(); //连胜 在加一星
      }, 500);
      if (this.bigMedalChange) { //升大段
        setTimeout(() => {
          this.handleHideOldMedal();
        }, 600);
      }else{
        setTimeout(() => {
          this.handleSmallMedalChange();//小段位变化
        }, 600);
      }
    },

    handleMedalDown() {
      if(this.bigMedalChange){
        this.shrinkImgBtn=true;
        setTimeout(() => {
          this.magnifyBtn=true;
        }, 300);
        setTimeout(() => {
          this.starList=[{act:true,down:false},{act:true,down:false},{act:true,down:false}];
        }, 800);
      }else{
        this.handleSmallMedalChange();
          setTimeout(() => {
          this.starList=[{act:true,down:false},{act:true,down:false},{act:true,down:false}];
        }, 600);
      }
    },

    handleLevelUp() { 
      this.levelUp = true;
      for (let i = 0; i < this.starList.length; i++) {
        if (!this.starList[i].act) {
          this.starList[i].act = true;
          if (this.eventOnce) {
            this.eventOnce = false; //执行一次
          }
          break;
        }
      }
    },

    handleSmallMedalChange(){ //小段变化
      this.smallMedalUp=true;
      setTimeout(() => {
        this.medalNum = this.medalInfo.paragraphName.slice(4);
      }, 300);
    },

    handleLevelDown() {
      this.starTrans = false;
      let last = 0;
      for (let i = 0; i < this.starList.length; i++) {
        if (this.starList[i].act) {
          last = i;
        }
      }
      this.starList[last].down = true;
    },

    checkWinStreak() {
      if (this.medalInfo.continueWinNum >= 3) {
        this.winStreak = true;
        setTimeout(() => {
          this.handleLevelUp();
        }, 400);
      }
    },

    handleHideOldMedal() {
      // 旧段位图标隐藏
      this.shrinkBtn = true;
      setTimeout(() => {
        this.shrinkImgBtn = true;
      }, 600);
    },

    handleShowNewMedal() {
      //新段位图标出现
      this.magnifyBtn = true;
      this.showLightMask = true;
      setTimeout(() => {
        this.showLightMask = false;
        setTimeout(() => {
          this.showLightBg = true;
          setTimeout(() => {
            this.starList = [
              { act: false, down: false },
              { act: false, down: false },
              { act: false, down: false }
            ];
            if (this.fullStar) {
              setTimeout(() => {
                this.handleLevelUp();
              }, 500);
            }
          }, 400);
        }, 300);
      }, 400);
    }

    //
  },
  mounted() {
    this.initStarList(); //执行一次初始化 旧的 段位星级列表
    this.initMedalName(); //初始化 段位名字和数字
  }
};
</script>

<style lang="scss" scoped>
.wrap {
  width: 100%;
  height: 100%;
  background: #fff;
}
.medal-box {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  background: rgba(#000, 0.7);
}
@mixin cssCenter {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.m-word-img {
  width: 226rpx;
  height: 66rpx;
  margin-bottom: 40rpx;
  opacity: 0;
  transition: all 0.3s ease;
}
.m-medal {
  width: 450rpx;
  height: 450rpx;
  position: relative;
}
.u-img {
  width: 450rpx;
  height: 450rpx;
  position: absolute;
  @include cssCenter;
  z-index: 2;
  & > img {
    width: 100%;
    height: 100%;
  }
  .u-medal-level {
    width: 100%;
    height: 72rpx;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 80rpx;
    img {
      width: 342rpx;
      height: 72rpx;
      position: absolute;
      @include cssCenter;
    }
    .u-medal-name {
      position: absolute;
      top: 50%;
      left: 110rpx;
      transform: translateY(-50%);
      font-size: 48rpx;
      font-weight: bold;
      color: #fff;
      line-height: 1;
      .u-medal-num {
        position: absolute;
        transform: translateY(-50%);
        top: 60%;
        right: -70rpx;
        letter-spacing: -12rpx;
        width: 72rpx;
        color: #f8eb7e;
        text-align: center;
      }
    }
  }
}

.u-light-mask {
  width: 380rpx;
  height: 380rpx;
  background: #f9f6c2;
  border-radius: 50%;
  filter: blur(20rpx);
  position: absolute;
  z-index: 3;
  @include cssCenter;
  opacity: 0;
  transition: all .3s ease;
}
.u-blue-cycle,
.u-blue-cycle-inner {
  width: 315rpx;
  height: 315rpx;
  box-sizing: border-box;
  border: 6rpx solid #fff;
  border-radius: 50%;
  opacity: 0;
  position: absolute;
  @include cssCenter;
}
.u-blue-cycle {
  z-index: 1;
  box-shadow: 0px 0px 36rpx 10rpx rgba(#5ff8fe, 0.9);
  .u-blue-cycle-inner {
    box-shadow: 0px 0px 36rpx 10rpx #5ff8fe inset;
  }
}
.u-light-bg {
  position: absolute;
  @include cssCenter;
  transform: translate(-50%, -65%);
  transition: all 0.8s ease-out;
  opacity: 1;
  img {
    width: 579rpx;
    height: 425rpx;
  }
}

// 星星列表
.m-star-list {
  margin-top: -25rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}
.u-star {
  position: relative;
  width: 51rpx;
  height: 51rpx;
  margin-right: 16rpx;
  background-size: 100%;
  background-repeat: no-repeat;
  background-image: url("");
  &:last-child {
    margin-right: 0;
  }
  .u-star-act {
    position: absolute;
    @include cssCenter;
    width: 66rpx;
    height: 64rpx;
    opacity: 0;
  }
  .transStar {
    transition: all 0.5s ease;
  }
  .showStar {
    opacity: 1;
  }
}
.u-win-streak {
  opacity: 0;
  font-size: 46rpx;
  color: #fff;
  text-align: center;
  margin-top: 70rpx;
  transition: all 0.4s ease;
}
// 动画集合
.shrink {
  animation: shrink .6s ease;
}
.shrinkImg {
  animation: shrinkImg 0.3s ease forwards;
}
.magnify {
  animation: magnify 0.4s ease forwards;
}
.downStar {
  animation: downStar 0.6s ease-out forwards;
}
.numChange{
  animation: numChange 0.6s ease forwards;
}
.hide {
  opacity: 0;
}
.show {
  opacity: 1;
}
.showHalf{
  opacity: .7;
}
// @keyframes
// 光圈动画
@keyframes shrink {
  0%,
  30.2%,
  62.2% {
    width: 480rpx;
    height: 480rpx;
    opacity: 0.6;
  }
  30%,
  62% {
    width: 280rpx;
    height: 280rpx;
    opacity: 1;
  }
  30.1%,
  62.1% {
    width: 480rpx;
    height: 480rpx;
    opacity: 0;
  }
  92.2%,
  100% {
    width: 340rpx;
    height: 340rpx;
    opacity: 0;
  }
}
//旧图标动画
@keyframes shrinkImg {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.7);
  }
}
//新图标动画
@keyframes magnify {
  0% {
    transform: translate(-50%, -50%) scale(0.7);
  }
  80% {
    transform: translate(-50%, -50%) scale(1.1);
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
  }
}
// 掉星动画
@keyframes downStar {
  10%,
  30% {
    transform: translate(-50%, -50%) skewY(10deg);
  }
  20%,
  40% {
    transform: translate(-50%, -50%) skewY(-10deg);
  }
  50% {
    transform: translate(-50%, -50%) skewY(0deg);
  }
  // 60%{
  //   transform: translate(-50%,-50%) skewY(0deg);
  // }
  100% {
    transform: translate(-50%, 150%) skewY(0deg);
    opacity: 0;
  }
}
// 生小段 数字变化
@keyframes numChange {
  0%{
    opacity: 1;
    transform:translateY(-50%) scale(1);
  }
  50%{
    opacity: 0;
    transform:translateY(-50%) scale(0);
  }
  100%{
    opacity: 1;
    transform:translateY(-50%) scale(1);
  }
}
</style>
